﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.Data.SqlClient;
using System.IO;
using Microsoft.SolverFoundation.Common;
using Microsoft.SolverFoundation.Solvers;
using Microsoft.SolverFoundation.Services;




namespace PlanningDepartment
{
    public partial class linear_programming : Telerik.WinControls.UI.RadForm
    {
        public linear_programming()
        {
            InitializeComponent();
        }
        private int i = 0,j=0;
        private string string_temp="";
        private void linear_programming_Load(object sender, EventArgs e)
        {
            
        }
        
        private void radButton1_Click(object sender, EventArgs e)
        {
           

            //GridViewDataColumn newCol = new GridViewDataColumn();
            try
            {
                int count_Var = int.Parse(txt_SoXe_KV1Search.Text);
                GridViewTextBoxColumn GhiChu = new GridViewTextBoxColumn();
                GhiChu.HeaderText = "";
                GhiChu.Name = "GhiChu";
                GhiChu.Width = 65;
                GhiChu.ReadOnly = true;
                radGridView2.Columns.Add(GhiChu);

                for (i = 0; i < count_Var; i++)
                {
                    GridViewTextBoxColumn newCol = new GridViewTextBoxColumn();
                    newCol.HeaderText = "X" + i.ToString();
                    newCol.Name = "x" + i.ToString();
                    //newCol.ReadOnly = false;
                    newCol.Width = 65;
                    radGridView2.Columns.Add(newCol);
                   
                    //newCol.Dispose();
                }
                GridViewTextBoxColumn BPT = new GridViewTextBoxColumn();
                BPT.HeaderText = ">=";
                BPT.Name = "BieuThuc";
                BPT.Width = 65;
                BPT.ReadOnly = true;
                radGridView2.Columns.Add(BPT);
                GridViewTextBoxColumn Limit = new GridViewTextBoxColumn();
                Limit.HeaderText = "Limit";
                Limit.Name = "Limit";
                Limit.Width = 80;
                //BPT.ReadOnly = true;
                radGridView2.Columns.Add(Limit);
                //radGridView3.Columns.Add(Limit);
                GridViewTextBoxColumn GhiChu1 = new GridViewTextBoxColumn();
                GhiChu1.HeaderText = "";
                GhiChu1.Name = "GhiChu";
                GhiChu1.Width = 65;
                GhiChu1.ReadOnly = true;
                radGridView3.Columns.Add(GhiChu1);
                for (i = 0; i < count_Var; i++)
                {
                    GridViewTextBoxColumn newCol = new GridViewTextBoxColumn();
                    newCol.HeaderText = "X" + i.ToString();
                    newCol.Name = "x" + i.ToString();
                    //newCol.ReadOnly = false;
                    newCol.Width = 65;
                    radGridView3.Columns.Add(newCol);

                    //newCol.Dispose();
                }

                GridViewTextBoxColumn Limit1 = new GridViewTextBoxColumn();
                Limit1.HeaderText = "Limit";
                Limit1.Name = "Limit";
                Limit1.Width = 60;
                //BPT.ReadOnly = true;
                radGridView3.Columns.Add(Limit1);
                GridViewTextBoxColumn min_value = new GridViewTextBoxColumn();
                min_value.HeaderText = "Min value";
                min_value.Name = "Min_value";
                min_value.Width = 80;
                //BPT.ReadOnly = true;
                radGridView3.Columns.Add(min_value);
                //radGridView3.Columns.Add(Limit);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void radButton2_Click(object sender, EventArgs e)
        {
            GridViewDataRowInfo rowInfo = new GridViewDataRowInfo(this.radGridView2.MasterView);
            if (radGridView2.Rows.Count > 0)
            {
                rowInfo.Cells[0].Value = "RB" + radGridView2.Rows.Count.ToString() ;
                for (i = 1; i < radGridView2.Columns.Count; i++)
                {
                    rowInfo.Cells[i].Value = 0;
                    if (i == radGridView2.Columns.Count - 2)
                        rowInfo.Cells[i].Value = ">=";

                }
                rowInfo.Height = 30;
                radGridView2.Rows.Add(rowInfo);
            }
            else
            {
                rowInfo.Cells[0].Value = "Min";
                for (i = 1; i < radGridView2.Columns.Count-2; i++)
                {
                    rowInfo.Cells[i].Value = 0;
                    

                }
                rowInfo.Cells[radGridView2.Columns.Count - 1].Value = 0;
                rowInfo.Height = 30;
                radGridView2.Rows.Add(rowInfo);
            }
        }

        private void radGridView2_DoubleClick(object sender, EventArgs e)
        {
            if (radGridView2.Rows.Count > 0)
            {
                int r = radGridView2.CurrentRow.Index;
                if(r>0)
                    radGridView2.Rows.RemoveAt(r);
            }
        }

        private void radButton3_Click(object sender, EventArgs e)
        {
            try
            {
                Process prs = new Process();
                DataTable dt = new DataTable();
                for (i = 1; i < radGridView2.Columns.Count; i++)
                {
                   
                        dt.Columns.Add(radGridView2.Columns[i].Name.ToString());
                    //MessageBox.Show(radGridView4.Columns[i].FieldName.ToString());
                    
                }
               
                for (i = 0; i < radGridView2.Rows.Count; i++)
                {
                    DataRow row = dt.NewRow();
                    for (j = 1; j < radGridView2.Columns.Count; j++)
                    {
                        
                            row[j-1] = radGridView2.Rows[i].Cells[j].Value;
                       
                        //MessageBox.Show(row[j-1].ToString());
                    }
                   
                    dt.Rows.Add(row);

                }
                //MessageBox.Show(dt.Rows.Count.ToString());

                //int[] a = new int[dt.Columns.Count - 1];
                //MessageBox.Show(a.Length.ToString());
               // prs.ToiUuHoa_int(dt);
                //double temp = 0;
                int row_ = dt.Rows.Count;
                int colomuns_=dt.Columns.Count;
                double[] Solution=new double[colomuns_-2];
                if (radDropDownList1.Text == "Integer")
                {
                    if(radDropDownList2.Text=="General")
                        Solution = prs.ToiUuHoa_int(dt);
                    if (radDropDownList2.Text == "Sawing")
                    {
                        Solution = ToiUuHoa_int_sawing(dt);
                        //MessageBox.Show("aaaa");
                    }

                }
                else
                {
                    Solution = prs.ToiUuHoa_Double(dt);
                }
                if (radGridView3.Rows.Count == 0)
                {
                    GridViewDataRowInfo rowInfo1 = new GridViewDataRowInfo(this.radGridView3.MasterView);
                    rowInfo1.Cells[0].Value = "Hệ số:";
                    for (i = 1; i < radGridView2.Columns.Count-2; i++)
                    {
                        rowInfo1.Cells[i].Value = radGridView2.Rows[0].Cells[i].Value;
                    }
                    radGridView3.Rows.Add(rowInfo1);
                }
                GridViewDataRowInfo rowInfo = new GridViewDataRowInfo(this.radGridView3.MasterView);
                
                    //rowInfo.Cells[0].Value = "RB" + radGridView2.Rows.Count.ToString();
                rowInfo.Cells[0].Value = "";
                for (i = 0; i < Solution.Length; i++)
                {
                    rowInfo.Cells[i+1].Value = Solution[i];
                       

                }
                rowInfo.Cells[i+1].Value = radGridView2.Rows[0].Cells[radGridView2.Columns.Count - 1].Value;
                
                double temp = 0;
                for (j = 0; j < dt.Columns.Count - 2; j++)
                {
                    temp += double.Parse(dt.Rows[0][j].ToString()) * Solution[j];
                }
                temp += double.Parse(dt.Rows[0][dt.Columns.Count - 1].ToString());
                rowInfo.Cells[i + 2].Value = temp;
                rowInfo.Height = 30;
                radGridView3.Rows.Add(rowInfo);
                
                
                /*string_temp = "Mô hình bài toán quy hoạch tuyến tính: \n";
                string_temp += "Hàm mục tiêu: MIN (";
                for (i = 0; i < colomuns_-2; i++)
                {
                    string_temp += dt.Rows[0][i].ToString() + " * X" + i.ToString()+"    ";
                }
                string_temp += "     "+dt.Rows[0][colomuns_ - 1].ToString()+") \n";

                
                for (i = 1; i < row_; i++)
                {
                   
                    for (j = 0; j < colomuns_-2; j++)
                    {
                        string_temp += dt.Rows[i][j].ToString() + " X" + j.ToString()+"      ";
                        //string_temp += "Hàm mục tiêu: MIN ("+dt.Rows[i][j].ToString;
                    }
                    string_temp += dt.Rows[i][colomuns_-2].ToString()+" "+dt.Rows[i][colomuns_-1].ToString()+" \n";
                }
                string_temp += "Nghiệm tối ưu: \n";
                for (i = 0; i < Solution.Length; i++)
                {
                    string_temp += "X" + i.ToString() + "=" + string.Format("{0:###,###,###.#####}", Solution[i].ToString()) + ", \n";
                    
                }
                for (i = 0; i < dt.Columns.Count - 2; i++)
                {
                    temp += double.Parse(dt.Rows[0][i].ToString())*Solution[i];
                }
                temp += double.Parse(dt.Rows[0][dt.Columns.Count - 1].ToString());
                string_temp += "Giá trị tối ưu đạt được là: " + temp.ToString() + " \n";
                string_temp += "Phòng Kế Hoạch - Khách Hàng - Scansia Pacific";
                //MessageBox.Show(string_temp);
                //radRichTextBox1.Text = string_temp;
                richTextBox1.Text = string_temp;*/
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void radButton4_Click(object sender, EventArgs e)
        {
            if (radGridView2.Columns.Count > 0)
            {
                int r = radGridView2.CurrentColumn.Index;
                if (r > 0 && r < radGridView2.Columns.Count - 2)
                {
                    radGridView2.Columns.RemoveAt(r);
                    radGridView3.Columns.RemoveAt(r);
                }
            }
        }
        public double[] ToiUuHoa_int_sawing(DataTable dt)
        {

            SimplexSolver solver = new SimplexSolver();
            int n = dt.Columns.Count - 2,i=0;
            int[] a = new int[n];
            for (i = 0; i < n; i++)
            {
                solver.AddVariable("X" + i.ToString(), out a[i]);
                solver.SetBounds(a[i], 0, Rational.PositiveInfinity);
                solver.SetIntegrality(a[i], true);//Lay NGHIEM NGUYEN
            }//Khi bao bien va rang buoc cua bien
            int rangbuoc = dt.Rows.Count+a.Length+1;
            int[] b = new int[rangbuoc]; //Tạo ràng buộc
            
            for (i = 0; i < dt.Rows.Count; i++)
            {

                solver.AddRow(i.ToString(), out b[i]);
                if (i == 0)
                {

                    for (int k = 0; k < n; k++)
                    {
                        //MessageBox.Show(dt.Rows[i][k].ToString());
                        //int_temp = int.Parse(dt.Rows[i][k].ToString());
                        //MessageBox.Show(int_temp.ToString());
                        solver.SetCoefficient(b[0], a[k], double.Parse(dt.Rows[i][k].ToString()));
                        // MessageBox.Show(dt.Rows[i][k].ToString());
                    }
                    solver.AddGoal(b[0], 1, true);
                    //essageBox.Show("xong ham muc tieu");
                }
                if (i > 0)
                {

                    for (int j = 0; j < n; j++)
                    {
                        solver.SetCoefficient(b[i], a[j], double.Parse(dt.Rows[i][j].ToString()));
                    }
                    solver.SetBounds(b[i], double.Parse(dt.Rows[i][dt.Columns.Count - 1].ToString()), Rational.PositiveInfinity);
                    //MessageBox.Show("them rang buoc "+i.ToString());
                }

            }
            int count = 0;
            for (i = dt.Rows.Count; i < b.Length; i++)
            {

                solver.AddRow(i.ToString(), out b[i]);
                if (i == dt.Rows.Count)
                {

                    for (int j = 0; j < n; j++)
                    {
                        solver.SetCoefficient(b[i], a[j], double.Parse(dt.Rows[0][j].ToString()));
                    }
                    solver.SetBounds(b[i], int.Parse(dt.Rows[0][dt.Columns.Count - 1].ToString())*(-1), Rational.PositiveInfinity);
                }
                else
                {
                    
                        solver.SetCoefficient(b[i], a[count], double.Parse(dt.Rows[0][count].ToString()));
                        solver.SetBounds(b[i], int.Parse(dt.Rows[0][dt.Columns.Count - 1].ToString())*(-1), Rational.PositiveInfinity);
                        count++;
                  //  MessageBox.Show((int.Parse(dt.Rows[0][dt.Columns.Count - 1].ToString())*(-1)).ToString());
                   
                }

            }
            SimplexSolverParams param = new SimplexSolverParams();
            param.MixedIntegerGenerateCuts = true;
            solver.Solve(param);
            double[] Solution = new double[a.Length];
            for (i = 0; i < a.Length; i++)
            {
                Solution[i] = solver.GetValue(a[i]).ToDouble();
            }
            return Solution;
            //MessageBox.Show("xong roi");
            /*for (int l = 0; l < a.Length; l++)
            {
                MessageBox.Show(solver.GetValue(a[l]).ToDouble().ToString());
            }*/
        }

        private void radGridView3_DoubleClick(object sender, EventArgs e)
        {
            if (radGridView3.Rows.Count > 0)
            {
                int r = radGridView3.CurrentRow.Index;
               
                    radGridView3.Rows.RemoveAt(r);
            }
        }

        private void Export_Excel_Click(object sender, EventArgs e)
        {
            try
            {
                Process prs = new Process();
                //DataTable dt = (DataTable)radGridView1.DataSource;
                if (radGridView3.Rows.Count > 0)
                {
                    prs.ExportExcel_grid(radGridView3, "C:\\Huynh\\Optimal_Solution.xlsx", 6, 1);
                }
                else
                    MessageBox.Show("Không có dữ liệu");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
       
    }
}
